package mapReduce.writable;

import org.apache.hadoop.io.Writable;
import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class PhoneTrafficWritable implements WritableComparable<PhoneTrafficWritable> {

    private int upTraffic;
    private int downTraffic;
    private int sumTraffic;

    @Override
    public void write(DataOutput out) throws IOException {
        out.writeInt(upTraffic);
        out.writeInt(downTraffic);
        out.writeInt(sumTraffic);
    }

    @Override
    public void readFields(DataInput in) throws IOException {
        //反序列化时顺序必须与序列化的顺序保持一致
        this.upTraffic = in.readInt();
        this.downTraffic = in.readInt();
        this.sumTraffic = in.readInt();
    }

    @Override
    public int compareTo(PhoneTrafficWritable o) {

        int thisSumTraffic = this.sumTraffic;
        int thatSumTraffic = o.getSumTraffic();

        int thisUpTraffic = this.upTraffic;
        int thatUpTraffic = o.getUpTraffic();

        if(thisSumTraffic==thatSumTraffic){
            return thisUpTraffic-thatUpTraffic;
        }else{
            return thatSumTraffic-thisSumTraffic;
        }
    }


    public int getUpTraffic() {
        return upTraffic;
    }

    public void setUpTraffic(int upTraffic) {
        this.upTraffic = upTraffic;
    }

    public int getDownTraffic() {
        return downTraffic;
    }

    public void setDownTraffic(int downTraffic) {
        this.downTraffic = downTraffic;
    }

    public int getSumTraffic() {
        return sumTraffic;
    }

    public void setSumTraffic(int sumTraffic) {
        this.sumTraffic = sumTraffic;
    }

    @Override
    public String toString() {
        return upTraffic + "\t" + downTraffic + "\t" + sumTraffic;
    }

}
